package com.share.um

import android.app.Activity
import com.umeng.socialize.ShareAction
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import com.umeng.socialize.shareboard.ShareBoardConfig
import com.umeng.socialize.shareboard.SnsPlatform
import com.umeng.socialize.utils.ShareBoardlistener

/**
 * Created by zhudongao
 * Desc
 * Date 2/26/21
 */
object ShareHelper {

    private val shareMap:MutableMap<String, ShareAction> = HashMap()

    fun register(activity: Activity):ShareAction {
        val shareListener = ShareListener(activity)
        return register(activity, shareListener)
    }

    fun register(activity: Activity, shareListener: ShareListener):ShareAction {
        val shareAction = ShareAction(activity)
            .setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE
            ).setShareboardclickCallback { snsPlatform, shareMedia ->
                run {
                    val web = UMWeb(SHARE_URL)
                    web.title = "分享标题"
                    web.description = "分享内容"
                    web.setThumb(UMImage(activity, R.drawable.ic_launcher_round))
                    ShareAction(activity).withMedia(web)
                        .setPlatform(shareMedia)
                        .setCallback(shareListener)
                        .share()
                }
            }
        shareMap[activity.hashCode().toString()] = shareAction
        return shareAction
    }

    fun openBottom(activity: Activity) {
        getAction(activity).open()
    }

    fun openCenter(activity: Activity) {
        val config = ShareBoardConfig()
        config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_CENTER)
        config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_NONE)
        getAction(activity).open(config)
    }

    fun openWithConfig(activity: Activity, config: ShareBoardConfig) {
        getAction(activity).open(config)
    }

    fun getAction(activity: Activity):ShareAction {
        var shareAction = shareMap[activity.hashCode().toString()]
        if (shareAction == null) {
            shareAction = register(activity)
        }
        return shareAction
    }
}